package com.bw.mallshop.mvvm.view;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.bw.common.field.ActionEntity;
import com.bw.common.mvvm.view.BaseActivity;
import com.bw.mallshop.BR;
import com.bw.mallshop.R;
import com.bw.mallshop.databinding.ActivitySkillvalueBinding;
import com.bw.mallshop.entity.SecKillEntity;
import com.bw.mallshop.field.TimeAction;
import com.bw.mallshop.mvvm.viewmodel.SKillValueViewModel;
import com.bw.mallshop.service.TimeService;

public class SkillValueActivity extends BaseActivity<ActivitySkillvalueBinding,
        SKillValueViewModel> implements Observer<TimeAction> {

    private TimeService service;

    @Override
    protected int createvariableId() {
        return BR.vm;
    }

    @Override
    public int bindlayout() {
        return R.layout.activity_skillvalue;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SecKillEntity.Value bean = (SecKillEntity.Value) getIntent().getExtras().getSerializable("skillBean");
        vm.bean = bean;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = new Intent(this, TimeService.class);
        bindService(intent,connection,BIND_AUTO_CREATE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unbindService(connection);
    }

    ServiceConnection connection =new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            TimeService.TimeBinder binder = (TimeService.TimeBinder) service;
            SkillValueActivity.this.service =  binder.getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    public void onChanged(TimeAction action) {
        if (action.action.equals(ActionEntity.GETTIME)){
            //activity调用service下维护的时间->调用vm下秒杀方法发起秒杀
            vm.requestSkill(service.getNowTime());
        }
    }
}
